﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WAContratos.Manager;
using WebContratos.Bean;

namespace WebContratos
{
    public partial class consultaGarantia : System.Web.UI.Page
    {

        public ContratoManager unContra = new ContratoManager();
        public GarantiaBean unGaran = new GarantiaBean();
        public ArrayList unArray = null;
        public string codContrato = "";
        public string codGarantia = "";
        public string tipo = "";
        public string secuencia = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!(IsPostBack))
                {

                        this.lblMontoSinMas.Attributes.Add("style", "display:none");
                    codContrato = Convert.ToString(Session["codContrato"]);
                    tipo = Convert.ToString(Request.QueryString["tipo"]);

                    if (tipo.Equals("actualiza"))
                    {
                        codGarantia = Convert.ToString(Request.QueryString["codGarantia"]);
                        secuencia = Convert.ToString(Request.QueryString["secuencia"]);

                        unContra = new ContratoManager();
                        unGaran = new GarantiaBean();

                        unArray = unContra.ObtieneGantiaContra(codContrato, codGarantia);

                        unGaran = (GarantiaBean)unArray[2];

                        this.lblTipo.Text = tipo;
                        this.txtCodGarantia.Text = unGaran.CodigoGarantia;
                        this.txtAseguradora.Text = unContra.obtieneAseguradora(unGaran.Aseguradora, "");
                        this.txtCodAseguradora.Text = unGaran.Aseguradora;
                        this.txtFechaInicio.Text = unGaran.FechaInicio;
                        this.txtFechaFin.Text = unGaran.FechaFin;
                        this.txtMonto.Text = unGaran.Monto;
                        this.txtPlazo.Text = unGaran.Plazo;
                        this.txtCodContrato.Text = codContrato;

                    }
                    else
                    {
                        this.txtCodContrato.Text = codContrato;
                        this.txtCodGarantia.Enabled = true;
                        this.txtCodGarantia.ReadOnly = false;
                    }


                }
            }
            catch (Exception e2)
            {
                MsgBox1.alert(e2.Message);
            }
        }

        protected void btnGrabar_Click(object sender, EventArgs e)
        {

            string sql = "";


            if (this.lblTipo.Text.Equals("actualiza"))
            {
                sql = "update t_garantia " +
                              " set tipo_garantia = '" + this.txtTipoGarantia.SelectedValue.ToString() + "'," +
                              "     aseguradora = '" + this.txtCodAseguradora.Text + "'," +
                              "     fecha_inicio = to_date('" + this.txtFechaInicio.Text + "','rrrr-mm-dd'), " +
                              "     plazo = '" + this.txtPlazo.Text + "'," +
                              "     monto = '" + this.txtMonto.Text + "'," +
                              "     fecha_fin = to_date('" + this.txtFechaFin.Text + "','rrrr-mm-dd')," +
                              "     estado = '" + this.txtEstado.SelectedValue.ToString() + "' " +
                              " where codigo_contrato = '" + this.txtCodContrato.Text + "' and codigo_garantia = '" + this.txtCodGarantia.Text + "'";

                cls_datos unDato = new cls_datos();

                bool result = unDato.insertaq(sql);

                /*if (result)
                    MsgBox1.alert("Se Actualizo Correctamente la Garantia No " + this.txtCodGarantia.Text);*/

                string jScript;
                jScript = "<script>consuGa('" + this.txtCodContrato.Text + "');</script>";
                //consuGaran('" + this.txtCodContrato.Text + "');</script>";



                //this.codContratoGar.Text = this.txtCodContrato.Text;


                Page.RegisterStartupScript("keyClientBlock", jScript);
            }
            else {

                if (this.txtTipoGarantia.SelectedValue.ToString().Equals("T"))
                {

                    GarantiaBean uGaranti = new GarantiaBean();

                    if (FileUpload1.HasFile)
                    {
                        try
                        {
                            FileUpload1.SaveAs(Server.MapPath("\\WAContratos\\documentos\\" + FileUpload1.FileName));
                            uGaranti.NomArchivo = FileUpload1.FileName;
                        }
                        catch (Exception ex)
                        {
                            String msgError = "";
                            String dato = "";
                            Console.Write(ex.Message);
                            msgError = ex.Message;

                            dato = "<script>alert('" + msgError + "');</script>";

                            Page.RegisterStartupScript("keyClienthabilita", dato);
                        }
                    }
                    else
                    {
                        uGaranti.NomArchivo = "";
                    }



                    uGaranti.CodigoContrato = this.txtCodContrato.Text;
                    uGaranti.CodigoGarantia = this.txtCodGarantia.Text;
                    uGaranti.TipoGarantia = this.txtTipoGarantia.SelectedValue.ToString();
                    uGaranti.Aseguradora = this.txtCodAseguradora.Text;
                    uGaranti.Monto = this.txtMonto.Text;
                    uGaranti.Plazo = this.txtPlazo.Text;
                    uGaranti.Estado = this.txtEstado.SelectedValue.ToString();
                    

                    ContratoManager uManager = new ContratoManager();
                    ErroresBean uError = new ErroresBean();

                    uError = uManager.guardarGarantias(uGaranti);

                    if (uError.P_cod_error.Equals("00"))
                    {
                        Session["msg_aplcacion"] = "Se inserto Correctamente el codigo de Garantia No." + this.txtCodGarantia.Text.ToUpper();
                        String cad = "<script> window.showModalDialog(\"mensajes/Mensaje.aspx\",'',' dialogWidth=450px; dialogHeight=180px;scroll=no; center=yes; help=no; status=no; menubar=no; resizable=no; border=thin');</script>";

                        Page.RegisterStartupScript("keyClientBlockAdendum", cad);

                    }
                    else
                    {
                        String cad = "<script> window.showModalDialog(\"mensajes/web_grl_Mensaje.aspx\",'',' dialogWidth=450px; dialogHeight=250px;scroll=no; center=yes; help=no; status=no; menubar=no; resizable=no; border=thin');</script>";
                        Session["cod_error_aplcacion"] = uError.P_cod_error;
                        Session["msg_error_aplcacion"] = uError.P_msg_error_aplica;
                        Session["msg_error_tecnico"] = uError.P_msg_error_tecni;
                        Session["nomAplicativo"] = "Error Consulta de Garantias";

                        Page.RegisterStartupScript("keyClientBlockAdendum", cad);
                    }



                  
                }
                else
                {


                    GarantiaBean uGaranti = new GarantiaBean();


                    if (FileUpload1.HasFile)
                    {
                        try
                        {
                            FileUpload1.SaveAs(Server.MapPath("\\WAContratos\\documentos\\" + FileUpload1.FileName));
                            uGaranti.NomArchivo = FileUpload1.FileName;
                        }
                        catch (Exception ex)
                        {
                            String msgError = "";
                            String dato = "";
                            Console.Write(ex.Message);
                            msgError = ex.Message;

                            dato = "<script>alert('" + msgError + "');</script>";

                            Page.RegisterStartupScript("keyClienthabilita", dato);
                        }
                    }
                    else
                    {
                        uGaranti.NomArchivo = "";
                    }


                    uGaranti.CodigoContrato = this.txtCodContrato.Text;
                    uGaranti.CodigoGarantia = this.txtCodGarantia.Text;
                    uGaranti.TipoGarantia = this.txtTipoGarantia.SelectedValue.ToString();
                    uGaranti.Aseguradora = this.txtCodAseguradora.Text;
                    uGaranti.Plazo = this.txtPlazo.Text;
                    uGaranti.Monto = this.txtMonto.Text;
                    uGaranti.Estado = this.txtEstado.SelectedValue.ToString();
                    uGaranti.FechaInicio = this.txtFechaInicio.Text;
                    uGaranti.FechaFin = this.txtFechaFin.Text;


                    ContratoManager uManager = new ContratoManager();
                    ErroresBean uError = new ErroresBean();

                    uError = uManager.guardarGarantias(uGaranti);



                    if (uError.P_cod_error.Equals("00"))
                    {
                        Session["msg_aplcacion"] = "Se inserto Correctamente el codigo de Garantia No." + this.txtCodGarantia.Text.ToUpper();
                        String cad = "<script> window.showModalDialog(\"mensajes/Mensaje.aspx\",'',' dialogWidth=450px; dialogHeight=180px;scroll=no; center=yes; help=no; status=no; menubar=no; resizable=no; border=thin');</script>";

                        Page.RegisterStartupScript("keyClientBlockAdendum", cad);

                    }
                    else
                    {
                        String cad = "<script> window.showModalDialog(\"mensajes/web_grl_Mensaje.aspx\",'',' dialogWidth=450px; dialogHeight=250px;scroll=no; center=yes; help=no; status=no; menubar=no; resizable=no; border=thin');</script>";
                        Session["cod_error_aplcacion"] = uError.P_cod_error;
                        Session["msg_error_aplcacion"] = uError.P_msg_error_aplica;
                        Session["msg_error_tecnico"] = uError.P_msg_error_tecni;
                        Session["nomAplicativo"] = "Error Consulta de Garantia";

                        Page.RegisterStartupScript("keyClientBlockAdendum", cad);
                    }
                   
                }
               
                /*if (result)
                    MsgBox1.alert("Se Actualizo Correctamente la Garantia No " + this.txtCodGarantia.Text);*/

                string jScript;
                jScript = "<script>consuGa('" + this.txtCodContrato.Text + "');</script>";
                //consuGaran('" + this.txtCodContrato.Text + "');</script>";



                //this.codContratoGar.Text = this.txtCodContrato.Text;


                Page.RegisterStartupScript("keyClientBlock", jScript);
            }

        }
    }
}
